package com.ias.agdis_r.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.terminalmanager.restful.workgroup.vo.Member;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 班组列表适配器
 * Created by zhaowl on 2018/4/3.
 */
public class WorkGroupListAdapter extends BaseAdapter {

    private Context context;
    private List<Member> listMember;
    private LayoutInflater inflater;
    private boolean isCheckMode;    // 是否开启选择模式

    public WorkGroupListAdapter(Context context, List<Member> listMember) {
        this.listMember = listMember;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listMember.size();
    }

    @Override
    public Member getItem(int position) {
        return listMember.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_work_group_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Member item = getItem(position);
        holder.numberTv.setText(item.getMember_usno());
        holder.nameTv.setText(item.getMember_name());

        if (!isCheckMode) {
            holder.nameTv.setCheckMarkDrawable(null);
        } else {
            holder.nameTv.setCheckMarkDrawable(holder.checkMarkDrawable);
            holder.nameTv.refreshDrawableState();
        }

        return convertView;
    }

    public void setCheckMode(boolean b) {
        isCheckMode = b;
    }

    /**
     * 获取选中的人员列表
     * @param checkedItemPositions
     * @param headerCount
     * @return
     */
    public List<Member> getCheckedMembers(SparseBooleanArray checkedItemPositions, int headerCount) {
        List<Member> members = new ArrayList<>();
        for (int i = 0; i < checkedItemPositions.size(); i++) {
            boolean value = checkedItemPositions.valueAt(i);
            if (value) {
                int key = checkedItemPositions.keyAt(i);
                int position = key - headerCount;
                members.add(listMember.get(position));
            }
        }
        return members;
    }

    public void removeMembers(List<Member> somebody) {
        if (somebody == null || somebody.isEmpty()) {
            return;
        }
        boolean hasRemove = false;
        for (Member member: somebody) {
            String memberNo = member.getMember_usno();

            Iterator<Member> iterator = listMember.iterator();
            while (iterator.hasNext()) {
                Member next = iterator.next();
                if (memberNo.equals(next.getMember_usno())) {
                    iterator.remove();
                    hasRemove = true;
                    break;
                }
            }
        }
        if (hasRemove) {
            notifyDataSetChanged();
        }
    }

    public void addMember(Member member) {
        if (member == null) {
            return;
        }
        // 加入列表
        this.listMember.add(member);
        notifyDataSetChanged();
    }

    private static class ViewHolder {
        TextView numberTv;
        Drawable checkMarkDrawable;
        CheckedTextView nameTv;

        public ViewHolder(View view) {
            this.numberTv = (TextView) view.findViewById(R.id.tv_number);
            this.nameTv = (CheckedTextView) view.findViewById(android.R.id.text1);
            if (checkMarkDrawable == null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    checkMarkDrawable = nameTv.getCheckMarkDrawable();
                } else {
                    // 小于android16版本
                }
            }
        }
    }

}
